﻿
$(function () {
    var newCtrl = $("#new_password");
    var compareCtrl = $("#compare_password");
    var oldCtrl = $("#old_password");
    var comparePwd = function () {
        var ctrl1 = newCtrl;
        var ctrl = compareCtrl;
        var error = ctrl.parent().next().find(".cudTs3");
        var errorCon = error.find(".tdCon");
        var select = ctrl.parent().next().find(".setup_info");
        if (ctrl.val() != ctrl1.val()) {
            errorCon.text("两次输入的密码不一致，请重新输入");
            App.Setup.redInput(ctrl[0]);
            error.show();
            select.hide();
            return false;
        }
        else if (ctrl.val().length < 6 || ctrl.val().length > 16) {
            errorCon.text("密码长度不正确，应为6～16个字符");
            App.Setup.redInput(ctrl[0]);
            error.show();
            select.hide();
            return false;
        }
        else if (!new RegExp("^([\\w\\~\\!\\@@\\#\\$\\%\\^\\&\\*\\(\\)\\+\\`\\-\\=\\[\\]\\\\{\\}\\|\\;\\'\\:\\\"\\,\\.\\/\\<\\>\\?]{6,16})$").test(ctrl.val())) {
            errorCon.text("密码请勿使用特殊字符");
            App.Setup.redInput(ctrl[0]);
            error.show();
            select.hide();
            return false;
        }
        else {
            select.show();
            App.Setup.normalInput(ctrl[0]);
            return true;
        }
    }
    App.Setup.BindInput(oldCtrl, "密码由6-16位半角(字母、数字、符号)组成，区分大小写", App.Setup.checkPWD);
    App.Setup.BindInput(newCtrl, "密码由6-16位半角(字母、数字、符号)组成，区分大小写", App.Setup.checkPWD);
    App.Setup.BindInput(compareCtrl, "密码由6-16位半角(字母、数字、符号)组成，区分大小写", comparePwd);
    newCtrl.keyup(function () {
        var showPower = $("#image_password img");
        if (!this.value)
            showPower.attr("src", "/Content/Image/pwd_no.gif");
        var power = App.Setup.pwdPower(this.value);
        if (power == 1) {
            showPower.attr("src", "/Content/Image/pwd_simple.gif");
        }
        else if (power == 2) {
            showPower.attr("src", "/Content/Image/pwd_middle.gif");
        }
        else if (power == 3) {
            showPower.attr("src", "/Content/Image/pwd_strong.gif");
        }

    });
    $("#submit_password").click(function () {
        if (this.locked)
            return false;
        var ispwd1 = App.Setup.checkPWD(oldCtrl);
        var ispwd2 = App.Setup.checkPWD(newCtrl);
        var ispwd3 = comparePwd();
        if (ispwd1 && ispwd2 && ispwd3) {
            var alink = this;
            this.locked = true;
            $.ajax(
       {
           url: "/Ajax/EditPwd",
           datatype: "json",
           cache: false,
           data: { newpwd: newCtrl.val(), oldpwd: oldCtrl.val() },
           type: "post",
           success: function (o) {
               if (o.Code == "A00001") {
                   App.showLoginDial();
               }
               else if (o.Code == "A00003") {
                   App.FullMiniTip(CodeList["A00003"], alink, 2000, 1);
               }
               else if (o.Code == "A00020") {
                   App.FullMiniTip(CodeList[o.Code], alink, 2000, 1);
               }
               else {
                   App.FullMiniTip(CodeList[o.Code], alink, 1000, 3);
               }
               alink.locked = false;
           },
           error: function (request) {
               App.FullMiniTip(CodeList["A00003"], alink, 2000, 1);
               alink.locked = false;
           }
       });
        }
        return false;
    });

});
   